//
//  AppDelegate.m
//  someDemo
//
//  Created by 上官惠阳 on 2016/11/30.
//  Copyright © 2016年 上官惠阳. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //给导航栏加背景图片
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"bg.png"] forBarMetrics:UIBarMetricsDefault];

    [self createString];
    [self createArray];
    [self createDictionary];

    return YES;
}
- (void)createString
{
    NSLog(@"/**********************************字符串************************************/");
    //字符串创建方式1
    NSString *str1 = @"我是字符串1";
    NSString *str2 = @"我是字符串2";

    //字符串创建方式2
    NSString *str3 = [NSString stringWithFormat:@"%@+%@+%d",str1,str2,123];
    int a = 1;
    int b = 2;
    NSString *str4 = [[NSString alloc] initWithString:[NSString stringWithFormat:@"我是字符串%d+我是字符串%d",a,b]];

    //打印字符串
    NSLog(@"str1 = %@",str1);
    NSLog(@"str2 = %@",str2);
    NSLog(@"str3 = %@",str3);
    NSLog(@"str4 = %@",str4);

    /*
     格式化输出的符号
     %@     对象
     %d, %i 整数
     %g, %f 浮点/双字
     %u     无符整形
     %ld   64位长整数（long long）
     %p     指针
     */

    //字符串比较用isEqualToString:函数
    BOOL result = [str1 isEqualToString:str2];
//    result == YES or result
//    result == NO or !result
    if (result) {
        NSLog(@"str1和str2字符串相同");
    }else{
        NSLog(@"str1和str2字符串不相同");
    }

    BOOL result2 = [str3 isEqualToString:str4];
    if (result2) {
        NSLog(@"str3和str4字符串相同");
    }else{
        NSLog(@"str3和str4字符串不相同");
    }

    //字符串分割
    NSString *nstring = @"美国,加拿大,澳大利亚,津巴布韦,埃及";
    NSArray *array = [nstring componentsSeparatedByString:@","];
    NSLog(@"arr = %@",array);
    for (int i = 0; i < [array count]; i++) {
        NSLog(@"string:%@", [array objectAtIndex:i]);
    }

    //判断某个字符串是否以某个字符串开头或结尾
    NSString *string1 = @"卡拉是条狗";
    NSString *string2 = @"狗狗叫卡拉";
    //字符串以开头比较
    if([string1 hasPrefix:@"1234"])
    {
        NSLog(@"字符串string1以卡拉开头");
    }
    //字符串以结尾比较
    if([string2 hasSuffix:@"卡拉"])
    {
        NSLog(@"string2字符串以卡拉结尾");
    }


    //搜索和替换
    NSString *string = @"我们是 gong产 主义 接班人";
    NSString *temp = @"gong产";
    NSString *me = @"我";
    NSRange rang = [string rangeOfString:temp];
    NSRange rang1 = [string rangeOfString:me];

    NSLog(@"搜索的字符串在string中起始点的index 为 %ld", rang.location);
    NSLog(@"搜索的字符串在string中结束点的index 为 %ld", rang.location + rang.length);
    NSLog(@"我 在字符串 string中的起点的index  为 %ld",rang1.location);

    //将搜索中的字符串替换成为一个新的字符串
    NSString *str = [string stringByReplacingCharactersInRange:rang withString:@"大产"];
    NSLog(@"替换后字符串为%@", str);

    //将字符串中" " 全部替换成 *
    str = [string stringByReplacingOccurrencesOfString :@" " withString:@"*"];
    NSLog(@"替换后字符串为%@", str);

    /*------------NSMutableString 可变字符串-----------------*/
    //创建
    NSMutableString *mStr1 = [[NSMutableString alloc] initWithFormat:@"abcd"];

    //给一个可变字符串赋值
    [mStr1 setString:@"123456789"];

    //插入
    [mStr1 insertString:@"defg" atIndex:4];    //注意：此处为在源字符串上修改，并未生成新的字符串
    NSLog(@"mStr1 = %@",mStr1);

    //替换
    [mStr1 replaceCharactersInRange:NSMakeRange(4, 4) withString:@"1234"];
    NSLog(@"%@",mStr1);

    //删除
    [mStr1 deleteCharactersInRange:NSMakeRange(4, 4)];
    NSLog(@"%@",mStr1);

    //后面拼接一个字符串
    [mStr1 appendString:@"hehehahalala"];
    NSLog(@"%@",mStr1);

    CGFloat c = 0.89;
    [mStr1 appendFormat:@"%@%d%g",str,a,c];
}
- (void)createArray
{
    NSLog(@"/**********************************数组************************************/");
    //类方法数组创建
    NSArray *array1 = [NSArray arrayWithObject:@"obj"];
    NSArray *array2 = [NSArray arrayWithObjects:@"obj1", @"obj2", @"obj3", nil];
    NSArray *array3 = [NSArray arrayWithArray:array2];
    NSLog(@"array1 :%@", array1);
    NSLog(@"array2 :%@", array2);
    NSLog(@"array3 :%@", array3);

    //实例方法创建数组
    NSArray *array4 = [[NSArray alloc] initWithObjects:@"AAA", @"bbb", nil];
    NSLog(@"array4 :%@", array4);

    //数组个数
    NSLog(@"array3 count :%ld", [array3 count]);

    //访问元素
    NSLog(@"obj at index :%@", [array2 objectAtIndex:2]);

    //追加(返回新的array对象)
    NSArray *array5 = [array3 arrayByAddingObject:@"ccc"];
    NSLog(@"array5 :%@", array5);

    //对数组直接赋值
    NSArray *array6 = @[@"11",@"22",@"33",@"44"];
    NSLog(@"arrary6:%@",array6);

    //根据指定的字符串连接数组元素
    NSString *joinString1 = [array5 componentsJoinedByString:@"|"];
    NSLog(@"joinString :%@",joinString1);

    //是否包含指定对象
    NSLog(@"isContains :%d", [array5 containsObject:@"obj2"]);


    //查找某个对象所在索引
    NSLog(@"indexOfObject :%ld",[array5 indexOfObject:@"obj3"]);

    //最后一个元素
    NSLog(@"lastObejct :%@", [array5 lastObject]);

    //遍历数组
    for (id element in array5) {
        NSLog(@"element :%@", element);
    }

    //可变数组(期望容量设置为3)
    NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:3];
    //直接添加
    [mutableArray addObject:@"aaa"];
    [mutableArray addObject:@"eee"];
    [mutableArray addObject:@"000"];
    NSLog(@"addObject :%@", mutableArray);

    //插入元素
    [mutableArray insertObject:@"ccc" atIndex:0];
    NSLog(@"insertObject :%@", mutableArray);

    //移除指定元素
    [mutableArray removeObject:@"ccc"];
    NSLog(@"removeObject :%@", mutableArray);

    //移除指定下标元素
    [mutableArray removeObjectAtIndex:0];
    NSLog(@"removeObjectAtIndex :%@", mutableArray);

    //移除最后一个元素
    [mutableArray removeLastObject];
    NSLog(@"removeLastObject :%@", mutableArray);

    //添加数组
    [mutableArray addObjectsFromArray:array5];
    NSLog(@"addObjectsFromArray :%@", mutableArray);

    //移除指定数组中的内容
    [mutableArray removeObjectsInArray:array2];
    NSLog(@"removeObjectsInArray :%@", mutableArray);

    //指定索引替换对象
    [mutableArray replaceObjectAtIndex:0 withObject:@"==="];
    NSLog(@"replaceObjectAtIndex :%@", mutableArray);

    //删除全部对象
    [mutableArray removeAllObjects];
    NSLog(@"removeAllObjects :%@", mutableArray);
}
- (void)createDictionary
{
    NSLog(@"/**********************************字典************************************/");
    //创建字典
    NSDictionary *dic1 = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];
    NSLog(@"dic1 :%@", dic1);

    //创建多个字典
    NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"value1", @"key1",
                          @"value2", @"key2",
                          @"value3", @"key3",
                          @"value4", @"key4",
                          nil];
    NSLog(@"dic2 :%@", dic2);


    //根据现有的字典创建字典
    NSDictionary *dic3 = [NSDictionary dictionaryWithDictionary:dic2];
    NSLog(@"dic3 :%@", dic3);

    //对字典直接赋值
    NSDictionary *dic4 = @{@"one":@"value1",@"two":@"value2",@"three":@"value3"};
    NSLog(@"dic4 :%@", dic4);

    //根据key获取value
    NSLog(@"key3 value :%@", [dic3 objectForKey:@"key3"]);

    //获取字典数量
    NSLog(@"dic count :%ld", dic3.count);

    //所有的键集合
    NSArray *keys = [dic3 allKeys];
    NSLog(@"keys :%@", keys);

    //所有值集合
    NSArray *values = [dic3 allValues];
    NSLog(@"values :%@", values);



    NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                       @"mvalue1", @"mkey1",
                                       @"mvalue2", @"mkey2", nil];
    //添加现有的字典数据
    [mutableDic addEntriesFromDictionary:dic3];
    NSLog(@"mutableDic :%@",mutableDic);

    //添加新的键值对象
    [mutableDic setValue:@"set1" forKey:@"setKey1"];
    NSLog(@"set value for key :%@",mutableDic);

    //以新的字典数据覆盖旧的字典数据
    [mutableDic setDictionary:dic2];
    NSLog(@" set dictionary :%@",mutableDic);

    //根据key删除value
    [mutableDic removeObjectForKey:@"key1"];
    NSLog(@"removeForkey :%@",mutableDic);

    //快速遍历
    for(id key in mutableDic) {
        NSLog(@"key :%@  value :%@", key, [mutableDic objectForKey:key]);
    }

    //枚举遍历
    NSEnumerator *enumerator = [mutableDic keyEnumerator];
    id key = [enumerator nextObject];
    while (key) {
        NSLog(@"enumerator :%@", [mutableDic objectForKey:key]);
        key = [enumerator nextObject];
    }


    //根据key数组删除元素
    [mutableDic removeObjectsForKeys:keys];
    NSLog(@"removeObjectsForKeys :%@",mutableDic);

    [mutableDic removeAllObjects];
    //删除所有元素
    NSLog(@"remove all :%@", mutableDic);
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
